﻿
using CommunicationProtocol.dto;
using CommunicationProtocol.dto.command.client;
using CommunicationProtocol.dto.command.server;
using monoClient.renderer;
using monoClient.utils;
using server.filter;
using server.filters;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunicationProtocol.dto.game;
using CommandExecutor = monoClient.renderer.CommandExecutor;
using monoClient.renderer.utils;

namespace monoClient.filter
{
    public class ServerCommandParser : NetFilter
    {

        OtherPlayerRendererHelper otherPlayerRendererHelper;

        public ServerCommandParser(ConcurrentDictionary<CommandExecutor,object> commandExecutors)
        {
            CommandExecutors = commandExecutors;
            otherPlayerRendererHelper= new OtherPlayerRendererHelper(commandExecutors);
        }

        public ConcurrentDictionary<CommandExecutor,object> CommandExecutors { get; }

        public void invoke(object buffer, InvocationChain invocationChain)
        {
            if (!(buffer is byte[])) {
                throw new ArgumentException();
            }

            byte[] data = (byte[])buffer;

            int functionType = data[0];

           

            byte[] remainingData = new ArraySegment<byte>(data, 1, data.Length - 1).ToArray();

            switch (functionType)
            {

                case 101:
                    {
                        // 创建用于攻击的植物
                        CreatingPlantCommand serverCommand = new CreatingPlantCommand(remainingData);

                        foreach (DynamicPlant dynamicPlant in serverCommand)
                        {
                            CommandExecutors.TryAdd(
                                new AnimatePlantRenderer(dynamicPlant),null
                            );
                        }
                        

                        break;
                    };
                case 102:
                    {
                        // 创建可选择的植物列表
                        RenderBuyablePlantListCommand serverCommand = new RenderBuyablePlantListCommand(remainingData);


                        foreach (StaticPlant plant in serverCommand.plants) {
                            CommandExecutors.TryAdd(
                                new BuyablePlantRenderer(plant),null
                                );
                        }

                        
                        break;
                    };
                case 103:
                    {
                        PlantBuyableStatusResp command = new PlantBuyableStatusResp(remainingData);

                        CommandExecutors.TryAdd(
                            new FollowingMousePictureRenderer(command, CommandExecutors),null
                        );
                        break;
                    };
                case 104: {
                        // 被要求登录
                        OutBandHolder.send(new LoginRequest("玩家ID "+ new Random().Next(100)));
                        break;
                    }
                case 6:
                {
                        // 渲染其他玩家的行为

                    otherPlayerRendererHelper.execute(new OtherPlayerPlantingCommand(remainingData));
                    break;
                }


                default:
                    {
                        throw new ArgumentException("不支持的功能类型" + functionType);

                    }
            }
          
        }
    }
}
